package com.tarena.elts.entity;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tarena.elts.util.Config;

public class EntityContext implements Serializable {
	private Config config;
	private Map<Integer, User> users = new HashMap<Integer, User>();
	private Map<Integer, List<Question>> questions = new HashMap<Integer, List<Question>>();

	public EntityContext(Config config) {
		this.config = config;
		loadUser(config.getString("UserFile"));
		loadQuestion(config.getString("QuestionFile"));
	}

	private void loadUser(String file) {
		// TODO Auto-generated method stub
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new BufferedInputStream(new FileInputStream(file)), "GBK"));
			String str;
			while ((str = br.readLine()) != null) {
				str = str.trim();
				if (str.equals("") || str.startsWith("#")) {
					continue;
				}
				User user = parseUser(str);
				users.put(user.getId(), user);
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new RuntimeException(e);
		}

	}

	private User parseUser(String str) {
		User user = new User();
		String[] data = str.split(":");
		user.setId(Integer.parseInt(data[0]));
		user.setName(data[1]);
		user.setPasswd(data[2]);
		user.setPhone(data[3]);
		user.setEmail(data[4]);
		return user;
	}

	public User getUserById(int id) {
		return users.get(id);
	}

	private void loadQuestion(String file) {
		// TODO Auto-generated method stub
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new BufferedInputStream(new FileInputStream(file)), "GBK"));
			String str;
			while ((str = br.readLine()) != null) {
				str = str.trim();
				if (str.equals("") || str.startsWith("#")) {
					continue;
				}
				Question q = parseQuestion(str, br.readLine(), br.readLine(),
						br.readLine(), br.readLine(), br.readLine());
				addQuestion(q);
			}
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public List<Question> getQuestion(int level) {
		return new ArrayList<Question>(questions.get(level));
	}

	private void addQuestion(Question q) {
		// TODO Auto-generated method stub
		List<Question> list = questions.get(q.getLevel());
		if (list == null) {
			list = new ArrayList<Question>();
			questions.put(q.getLevel(), list);
		}
		list.add(q);
	}

	private Question parseQuestion(String str, String readLine,
			String readLine2, String readLine3, String readLine4,
			String readLine5) {
		// TODO Auto-generated method stub
		Question question = new Question();
		String[] data = str.split("[@,][a-z]+=");
		question.setAnswers(parseAnswer(data[1]));
		question.setScore(Integer.parseInt(data[2]));
		question.setLevel(Integer.parseInt(data[3]));
		question.setTitle(readLine);
		List<String> options = new ArrayList<String>();
		options.add(readLine2);
		options.add(readLine3);
		options.add(readLine4);
		options.add(readLine5);
		question.setOptions(options);
		return question;
	}

	private List<Integer> parseAnswer(String str) {
		// TODO Auto-generated method stub
		List<Integer> answers = new ArrayList<Integer>();
		String[] data = str.split("/");
		for (String string : data) {
			answers.add(Integer.parseInt(string));
		}
		return answers;
	}

	public int getTimeLimit() {
		return config.getInt("TimeLimit");
	}

	public String getTitle() {
		return config.getString("PaperTitle");
	}

	public static void main(String args[]) {
		Config config = new Config("client.properties");
		EntityContext context = new EntityContext(config);
		System.out.println(context.users);
		System.out.println(context.questions);
	}
}
